<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_category\Entity\ShusyCategory;
use Drupal\shusy_index_category\Entity\ShusyIndexCategory;

/**
 * @OA\Schema(
 *   schema="CategoryItemIcon",
 *   type="object",
 *   description="CategoryItemIcon"
 * )
 */
class CategoryItemIcon extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_category';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyCategory) {
      $value = [
        /**
         * @OA\Property(
         *   property="id",
         *   type="integer",
         *   description="ID",
         * ),
         */
        'id' => (int) $this->entity->id,
        /**
         * @OA\Property(
         *   property="name",
         *   type="string",
         *   description="分类名",
         * ),
         */
        'name' => (string) $this->entity->name,
        /**
         * @OA\Property(
         *   property="icon",
         *   type="string",
         *   description="分类图标",
         * ),
         */
        'icon' => (string) $this->imageStyleUrl('CategoryItemIcon', file_load($this->entity->icon())),
        'query' => $this->getQuery($this->entity),
      ];
      $this->merge($value);
    }
  }

  protected function getQuery(ShusyCategory $category) {
    $query = '';
    switch ($category->depth) {
      case 0:
        $query = "depth_1:{$category->depth_1}";
        break;
      case 1:
        $query = "depth_2:{$category->depth_2}";
        break;
      case 2:
        $query = "depth_3:{$category->depth_3}";
        break;
    }
    return $query;
  }
}
